<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Thanhvien extends MY_Controller {

	public $data 	= 	array();
 	public $_template = array();
	public function __construct() {
		//parent::Controller();
		parent::__construct(); //for CI 2.x users 
		$this->load->module_model('member', 'qlsv_model', 'thanhvien');
		$this->load->module_language('member', 'qlsv');
		$this->load->helper('text');
		$this->user = $this->access->get_user();
		$this->load->helper('url');
		$this->access->restrict();
	}
	
 
	public function index() 
	{
		$data['ItemID'] = 'User';
		$data['title'] = 'Danh sách Thành viên';
		$data['description'] = 'Quản lý danh sách Thành viên';
		
		$this->access->restrict();
		$this->load->library('basic_library');
		$this->load->library('pagination');
		
		$config = array();
		$config['base_url'] = base_url().'member/thanhvien/';
		$config['total_rows'] = $this->thanhvien->count_all_user();
		$config['per_page'] = 30;
		$config['uri_segment'] = 3;
		$this->pagination->initialize($config);
		$page = ($this->uri->segment(3))? $this->uri->segment(3):0;
		
		$data['thanhviens'] = $this->thanhvien->get_all_thanhvien($config['per_page'], $page);
		$data['links'] = $this->pagination->create_links();		
		
		$this->_template['page']	= 'users/thanhvien/list';
		$this->system_library->load($this->_template['page'], $data);
	}
	
	public function add()
	{
		$data['ItemID'] = 'User';
		$data['title'] = 'Thêm người dùng mới';
		$data['description'] = 'Thêm người dùng mới';

		$this->load->helper('form');
		//$data['lops'] = $this->lop->get_all_lop();
		$userid = $this->user->id;
		$lang_working = load_language_working();
		
		//here is validation form
		$this->form_validation->set_rules('username', 'lang:form_username', 'required|callback_check_username');
		$this->form_validation->set_rules('fullname', 'lang:form_fullname', 'required|xss_clean|trim');
		$this->form_validation->set_rules('email', 'lang:form_email', 'xss_clean|trim|callback_check_email|valid_email');
		$this->form_validation->set_rules('password','lang:form_password','required|trim');
		$this->form_validation->set_rules('repassword','lang:form_repassword','required|matches[password]|trim');
		$this->form_validation->set_error_delimiters('', '<br />');

		if ($this->form_validation->run() == TRUE)
		{		
			$this->thanhvien->add_user();
			$this->session->set_flashdata('message', lang('successfully_created'));	
			redirect('member/thanhvien/', 'refresh');
		}	
		
		$this->_template['page']	= 'users/thanhvien/add';
		$this->system_library->load($this->_template['page'], $data);
	}
	
	public function check_username()
	{
        if($this->thanhvien->check_username())
        {
            $this->form_validation->set_message('check_username','Tài khoản đã tồn tại, vui lòng chọn lại');
            return false;
        }
        else
        {
            
            return true;
        }
    }
    
	public function check_email()
	{
        if($this->thanhvien->check_email())
        {
            $this->form_validation->set_message('check_email','Email đã tồn tại, vui lòng chọn lại');
            return false;
        }
        else
        {
            
            return true;
        }
    }
    
	
	public function delete($id)
	{
		$this->thanhvien->delete_user($id);
		$this->session->set_flashdata('message', lang('successfully_deleted'));
		redirect('member/thanhvien', 'refresh');
	}

	//Edit product
	public function edit($id)
	{
		//metadata
		$data['ItemID']		 = 'User';
		$data['title']		 = "Sửa thông tin người dùng"; 
		$data['description'] = "Sửa thông tin người dùng"; 
		$data['userinfo'] 	= $this->thanhvien->get_user_info($id);
		
		
		/*if($data['info']->masv==$this->input->post('masv'))
		{
			$this->form_validation->set_rules('masv', 'lang:form_masv', 'required|max_length[50]|xss_clean');
		}
		else
		{
			$this->form_validation->set_rules('masv', 'lang:form_masv', 'required|max_length[50]|xss_clean|callback_check_masv');
		}*/
		//echo $data['userinfo']->username;
		//echo $data['userinfo']->id;
		if($data['userinfo']->username == $this->input->post('username'))
		{
			$this->form_validation->set_rules('username', 'lang:form_username', '');
		}
		else 
		{
			$this->form_validation->set_rules('username', 'lang:form_username', 'required|callback_check_username');
		}
		if($data['userinfo']->email == $this->input->post('email'))
		{
			$this->form_validation->set_rules('email', 'lang:form_email', '');
		}
		else
		{
			$this->form_validation->set_rules('email', 'lang:form_email', 'xss_clean|trim|callback_check_email|valid_email');
		}
		$this->form_validation->set_rules('fullname', 'lang:form_fullname', 'required|xss_clean|trim');
		
		if($this->input->post('password')=='')
		{
			$this->form_validation->set_rules('password','lang:form_password','');
			$this->form_validation->set_rules('repassword','lang:form_repassword','');
		}
		else
		{
			$this->form_validation->set_rules('password','lang:form_password','required|trim');
			$this->form_validation->set_rules('repassword','lang:form_repassword','required|matches[password]|trim');
		}
		
		
					
		$this->form_validation->set_error_delimiters('', '<br />');
		
		if($this->form_validation->run() == TRUE)
		{
			$this->thanhvien->edit_user($id);
			$this->session->set_flashdata('message', lang('successfully_edited'));	
			redirect('member/thanhvien/', 'refresh');	
		}	
		$this->_template['page']	= 'users/thanhvien/edit';
		$this->system_library->load($this->_template['page'], $data);
	}
	
	public function enable($id = null)
	{
		if ($id == null)
		{
			$id = $this->input->post('id');
		}
			
		$this->thanhvien->change_status_user($id,1);
		redirect('member/thanhvien', 'refresh');
		
	}
	public function disable($id = null)
	{
		if ($id == null)
		{
			$id = $this->input->post('id');
		}
			
		$this->thanhvien->change_status_user($id,0);
		redirect('member/thanhvien', 'refresh');
	}
	
}

/* End of file welcome.php */
/* Location: ./application/controllers/products.php */